<!--合作单位设置-->
<template>
  <zz-dialog
    :title= cooperatorDetailTitle
    :visible = visible
    @close="close"
    @confirm="submitInfo"
    confirmVal = '保存'
    :showConfirm = true>
    <div v-bar class="tableClass" style="width: 900px; height: 400px">
      <div>
        <table class="cooper-table">
          <tr>
            <th>企业名称</th>
            <td><input v-validation="{required: true, query: 'companyName'}" v-model="companyName" class="input-n270"></td>
            <th>企业类型</th>
            <td>
              <zz-select
                style="width: 100%"
                :options="{width: '100%'}"
                :option="companyTypeList"
                :name = companyTypeName
                @optionClick="companyTypeCallback">
              </zz-select></td>
          </tr>
          <tr>
            <th>社会统一信用代码</th>
            <td colspan="3"><input v-model="creditCode" class="input-n270"></td>
          </tr>
          <tr>
            <th>注册日期</th>
            <td>
              <zz-date
                style="width: 100%"
                :clearPosition="clearPosition"
                @change="chooseregistrationDate"
                :place="registrationDatePlace"
                type="day"></zz-date></td>
            <th>注册所在地</th>
            <td><input v-model="registeredPlace" class="input-n270"></td>
          </tr>
          <tr>
            <th>注册资本（万元）</th>
            <td><input v-model="registeredCapital" class="input-n270"></td>
            <th>企业性质</th>
            <td>
              <zz-select
                style="width: 100%"
                :options="{width: '100%'}"
                :option="companyNatureList"
                :name = companyNatureName
                @optionClick="companyNatureCallback">
              </zz-select></td>
          </tr>
          <tr>
            <th>法定代表人</th>
            <td><input v-model="legalPerson" class="input-n270"></td>
            <th>营业地址</th>
            <td><input v-model="businessAddress" class="input-n270"></td>
          </tr>
          <tr>
            <th>联系人</th>
            <td><input v-model="linkman" class="input-n270"></td>
            <th>联系电话</th>
            <td><input v-model="contacts_mobile" class="input-n270"></td>
          </tr>
          <tr>
            <th>办公电话</th>
            <td><input v-model="mobile" class="input-n270"></td>
            <th>传真号</th>
            <td><input v-model="fax" class="input-n270"></td>
          </tr>
          <tr>
            <th>备注</th>
            <td colspan="3"><input v-model="remark" class="input-n270"></td>
          </tr>
          <tr>
            <td colspan="4"><span style="padding-left: 28px">添加班组</span><span class="right color" @click="groups++">+添加</span></td>
          </tr>
          <tbody ref="groups">
          <addGroup
            @set="setAddGroup"
            @del="delAddGroup"
            :key="n"
            :group="editGroup[n - 1]"
            v-for="n in groups"
          ></addGroup>
          </tbody>
        </table>
      </div>
    </div>
  </zz-dialog>
</template>

<script>
  import addGroup from './addGroup.vue'
  export default {
    name: 'cooperator',
    components: {
      addGroup
    },
    data () {
      return {
        groups: 1,
        companyName: '',
        cooperatorDetailTitle: '新增参建单位',
        cooperator_id: '',
        unitName: '',
        address: '',
        editGroup: [],
        groupMap: new Map(),
        groupName: [],
        companyType: '',
        companyTypeName: '请选择企业类型',
        companyTypeList: {},
        businessLicense: '',
        organizationCode: '',
        creditCode: '',
        registrationDate: '',
        registrationDatePlace: '',
        registeredPlace: '',
        registeredCapital: '',
        companyNature: '',
        companyNatureName: '请选择企业性质',
        companyNatureList: {
          0: '企业',
          1: '事业单位',
          2: '政府',
          3: '总包企业下属机构',
          4: '其它行政机关'
        },
        legalPerson: '',
        businessAddress: '',
        linkman: '',
        mobile: '',
        contacts_mobile: '',
        fax: '',
        remark: '',
        clearPosition: {
          top: 4,
          right: 10
        }
      }
    },
    props: {
      visible: {
        type: Boolean,
        default: false
      },
      pjId: '',
      cooperatorId: ''
    },
    watch: {
      visible (status) {
        if (status) {
          this.getCompanyType()
          if (this.cooperatorId) {
            this.getCooperator()
          }
        } else {
          Object.assign(this.$data, this.$options.data())
        }
      }
    },
    methods: {
      close () {
        this.$emit('close')
      },
      setAddGroup (data) {
        if (data.name) {
          this.groupMap.set(data.name, data)
        } else if (data.gId) {
          this.groupMap.set(data.gId, data)
        }
      },
      delAddGroup (key) {
        this.groupMap.delete(key)
      },
      getCompanyType () {
        this.$axios({
          method: 'post',
          url: `${this.$site.realServer}/front/person/findCompanyTypeList.htm`
        }).then(res => {
          if (res.data.errorMsg) {
            this.$message.error(res.data.errorMsg)
          } else {
            res.data.list.forEach(x => {
              this.companyTypeList[x.dict_id] = x.dict_value
            })
          }
        })
      },
      getCooperator () {
        const data = {
          'cooperator.cooperator_id': this.cooperatorId
        }
        this.$axios({
          method: 'post',
          url: `${this.$site.projectServer}/front/person/getCooperator.htm`,
          data: this.$qs.stringify(data)
        }).then(res => {
          if (res.data.errorMsg) {
            this.$message.error(res.data.errorMsg)
          } else {
            this.editCooperator(res.data.coo, res.data.list)
          }
        }, err => {
          this.$message.error(err)
        })
      },
      editCooperator (item, groupList) {
        this.cooperatorDetailTitle = '编辑参建单位'
        this.visible = true
        this.cooperator_id = item.cId
        this.companyName = item.name
        this.companyType = item.unit_type
        this.companyTypeName = this.companyTypeList[this.companyType]
        this.creditCode = item.credit_code
        if (item.register_date) {
          this.registrationDate = this.$moment(item.register_date).format('YYYY-MM-DD')
          this.registrationDatePlace = this.$moment(item.register_date).format('YYYY-MM-DD')
        }
        this.registeredPlace = item.register_address
        this.registeredCapital = item.register_cost
        this.companyNature = item.unit_property
        this.companyNatureName = this.companyNatureList[this.companyNature]
        this.legalPerson = item.unit_person
        this.businessAddress = item.unit_address
        this.linkman = item.contacts_name
        this.contacts_mobile = item.contacts_mobile
        this.mobile = item.mobile
        this.fax = item.fax
        this.remark = item.remark
        groupList.forEach(x => {
          this.groupMap.set(x.name, {
            name: x.name,
            gId: x.gId
          })
        })
        for (let i = 0, len = groupList.length; i < len; i += 2) {
          this.editGroup.push(groupList.slice(i, i + 2))
        }
        this.groups = this.editGroup.length
      },
      findWorkerByCooperator () {
        return new Promise((resolve) => {
          const data = {
            'query.cooperator_id': this.cooperatorId
          }
          this.$axios({
            method: 'post',
            url: `${this.$site.projectServer}/front/person/findWorkerByCooperator.htm`,
            data: this.$qs.stringify(data)
          }).then(res => {
            if (res.data.errorMsg) {
              resolve(res.data.errorMsg)
            } else {
              resolve(res.data.count)
            }
          })
        })
      },
      delCooperator () {
        this.$MessageBox({
          showClose: true,
          message: '你确定要解除吗？',
          type: 'info',
          confirmHanler: () => {
            this.findWorkerByCooperator().then(res => {
              if (res > 0) {
                this.$message.error('工人数大于0，不可删除')
              } else {
                const data = {
                  'cooperator.cooperator_id': this.cooperatorId,
                  'cooperator.status': 1
                }
                this.$axios({
                  method: 'post',
                  url: `${this.$site.projectServer}/front/person/doRelaseCooperation.htm`,
                  data: this.$qs.stringify(data)
                }).then(res => {
                  if (res.data.errorMsg) {
                    this.$message.error(res.data.errorMsg)
                  } else {
                    this.$message.success('操作成功!')
                    this.$emit('doRelase')
                  }
                })
              }
            })
          }
        })
      },
      submitInfo () {
        this.$vaildation(this).then(() => {
          let requestUrl = `${this.$site.realServer}/front/person/addCooperatorForWeb.htm`
          let data = {}
          this.groupName = [...this.groupMap.values()]
          if (this.cooperator_id) {
            data = {
              'form.cooperator_id': this.cooperator_id,
              'form.unit_name': this.companyName, // 企业名称
              'form.unit_type': this.companyType, // 企业类型
              'form.credit_code': this.creditCode, // 社会统一信用代码
              'form.register_date': this.registrationDate, // 注册日期
              'form.register_address': this.registeredPlace, // 注册所在地
              'form.register_cost': this.registeredCapital, // 注册资本
              'form.unit_property': this.companyNature, // 企业性质
              'form.unit_person': this.legalPerson, // 法定代表人
              'form.unit_address': this.businessAddress, // 营业地址
              'form.contacts_name': this.linkman, // 联系人
              'form.contacts_mobile': this.contacts_mobile, // 联系电话
              'form.mobile': this.mobile, // 办公电话
              'form.fax': this.fax, // 传真号
              'form.remark': this.remark, // 备注
              'form.groupName': JSON.stringify(this.groupName)
            }
          } else {
            data = {
              'form.project_id': this.pjId,
              'form.unit_name': this.companyName, // 企业名称
              'form.unit_type': this.companyType, // 企业类型
              'form.credit_code': this.creditCode, // 社会统一信用代码
              'form.register_date': this.registrationDate, // 注册日期
              'form.register_address': this.registeredPlace, // 注册所在地
              'form.register_cost': this.registeredCapital, // 注册资本
              'form.unit_property': this.companyNature, // 企业性质
              'form.unit_person': this.legalPerson, // 法定代表人
              'form.unit_address': this.businessAddress, // 营业地址
              'form.contacts_name': this.linkman, // 联系人
              'form.contacts_mobile': this.contacts_mobile, // 联系电话
              'form.mobile': this.mobile, // 办公电话
              'form.fax': this.fax, // 传真号
              'form.remark': this.remark, // 备注
              'form.groupName': JSON.stringify(this.groupName)
            }
          }
          this.$axios({
            method: 'post',
            url: requestUrl,
            data: this.$qs.stringify(data, {indices: false})
          }).then(res => {
            if (res.data.errorMsg) {
              this.$message.error(res.data.errorMsg)
            } else {
              this.close()
              this.$emit('success')
              this.$message.success('操作成功!')
            }
          })
        })
      },
      companyTypeCallback (data) {
        this.companyType = data.key
      },
      companyNatureCallback (data) {
        this.companyNature = data.key
      },
      chooseregistrationDate (data) {
        this.registrationDate = data
      }
    }
  }
</script>
<style lang="scss" scoped>
  .cooper-table{
    tr{
      height: 38px;
      th{
        text-align: center;
      }
    }
  }
</style>
